﻿using System;
using System.Collections;
using ExFlash.Tags;

namespace ExFlash.Records
{
    public class MorphLineStyleArray : ISwfSerializable, ICollection
    {
        private readonly TagCodes _code;

        public ushort LineStyleCount
        {
            get;
            protected set;
        }

        public MorphLineStyle[] LineStyles
        {
            get;
            protected set;
        }

        public MorphLineStyleArray(TagCodes code)
        {
            _code = code;
        }

        #region ISwfSerializable Members

        public void SwfDeserialize(BinaryBitReader reader)
        {
            LineStyleCount = reader.ReadByte();

            if (LineStyleCount == 0xFF)
            {
                LineStyleCount = reader.ReadUInt16();
            }

            LineStyles = new MorphLineStyle[LineStyleCount];

            if (_code == TagCodes.DefineMorphShape)
            {
                for (int i = 0; i < LineStyleCount; i++)
			    {
                    LineStyles[i] = new MorphLineStyle();
                    LineStyles[i].SwfDeserialize(reader);
			    }
            }
            else
            {
                for (int i = 0; i < LineStyleCount; i++)
                {
                    LineStyles[i] = new MorphLineStyle2();
                    LineStyles[i].SwfDeserialize(reader);
                }
            }
        }

        public void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return LineStyles.GetEnumerator();
        }

        #endregion

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            LineStyles.CopyTo(array, index);
        }

        public bool IsSynchronized
        {
            get { return LineStyles.IsSynchronized; }
        }

        public object SyncRoot
        {
            get { return LineStyles.SyncRoot; }
        }

        public int Count
        {
            get { return LineStyleCount; }
        }

        #endregion
    }
}
